package com.example.campustalent

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.campustalent.bean.UserInfoBean
import com.example.campustalent.databinding.ActivityLoginBinding
import com.example.campustalent.databinding.ActivityMainBinding
import com.example.campustalent.fragment.MeFragment
import com.example.campustalent.fragment.StudyFragment
import com.example.campustalent.utils.DbUtils
import com.example.campustalent.utils.getColorStateList
import com.example.campustalent.view.PerfectInformationDialog
import java.util.ArrayList

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private var mName: String? = null
    private val fragments: MutableList<Fragment> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        mName = intent.getStringExtra("name")
        val rootView: View = binding.root
        setContentView(rootView)
        initBottom()
    }

    private fun initBottom(){
        val userInfoBean = DbUtils.getQueryByWhere(UserInfoBean::class.java, "name", arrayOf(mName!!))[0]
        fragments.add(StudyFragment.newInstance(userInfoBean.isFirst, userInfoBean.name!!))
        fragments.add(MeFragment.newInstance(userInfoBean.isFirst, userInfoBean.name!!))
        binding.mainViewpager.apply {
            offscreenPageLimit = 2
            adapter = PagerAdapter(supportFragmentManager, fragments)
        }
        binding.mainBottom.apply {
            enableAnimation(true)
            enableShiftingMode(false)
            enableItemShiftingMode(true)
            itemIconTintList = getColorStateList(this@MainActivity)
            itemTextColor = getColorStateList(this@MainActivity)
            setTextSize(12F)
            setOnNavigationItemSelectedListener {
                when (it.itemId) {
                    R.id.menu_study -> {
                        binding.mainViewpager.setCurrentItem(0, false)
                    }
                    R.id.menu_me ->{
                        binding.mainViewpager.setCurrentItem(1, false)
                    }
                }
                true
            }
        }
        if (userInfoBean.isFirst){
            PerfectInformationDialog(this).apply {
                showPopupWindow()
                setOnItemClick(object : PerfectInformationDialog.OnItemClick{
                    override fun confirm() {
                        userInfoBean.isFirst = false
                        DbUtils.update(userInfoBean)
                        dismiss()
                        binding.mainBottom.currentItem = 1
                        binding.mainViewpager.setCurrentItem(1, false)
                    }

                })
            }
        }

    }

    @SuppressLint("WrongConstant")
    inner class PagerAdapter constructor(fm: FragmentManager, var fragments: List<Fragment>) :
        FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        override fun getItem(position: Int): Fragment {
            return fragments[position]
        }

        override fun getCount(): Int {
            return fragments.size
        }
    }
}